package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.bean.PayType;
import com.yc.biz.PayTypeBiz;
import com.yc.biz.impl.PayTypeBizImpl;

@WebServlet("/payTypeServlet")
public class PayTypeServlet extends HttpServlet {

	PayTypeBiz pb=new PayTypeBizImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		
		String op=req.getParameter("op");
		
		if(op.equals("updateMoney")){
			updateMoney(req,resp,out);
		}
	}

	//充值
	private void updateMoney(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		double price=Double.parseDouble( req.getParameter("price") );
		String paytype=req.getParameter("paytype");
		
		int result=pb.updateMoney(uid, price, paytype);
		if(result>0){
			out.print(1);
			//充值成功应该重新查询一下PayType
			List<PayType> list=pb.findAll(uid);
			req.getSession().setAttribute("payType", list);
		}else{
			out.print(0);
		}
	}

}
